package cn.xclink.kernel.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.xclink.common.mapper.BaseMapper;
import cn.xclink.common.service.BaseService;
import cn.xclink.kernel.entity.Resource;
import cn.xclink.kernel.entity.RoleResource;
import cn.xclink.kernel.entity.UserResource;
import cn.xclink.kernel.mapper.ResourceMapper;
import cn.xclink.kernel.mapper.RoleResourceMapper;
import cn.xclink.kernel.mapper.UserResourceMapper;

@Service
public class ResourceService extends BaseService<Resource> {

	@Autowired
	private ResourceMapper mapper;
	
	@Autowired
	private RoleResourceMapper roleResourceMapper;
	
	@Autowired
	private UserResourceMapper userResourceMapper;

	@Override
	public BaseMapper<Resource> getMapper() {
		return mapper;
	}
	
	/**
	 * 重写条件查询的方法
	 */
	@Override
	public List<Resource> find(Resource res) {
		return mapper.selectResource(res);
	}
	
	@Override
	public int delete(Object key) throws Exception {
		
		Long resourceId =  Long.parseLong(key+"");
		//1.校验是否有角色和用户使用该资源，如果资源被使用不允许删除
		RoleResource rr = new RoleResource();
		rr.setResourceId(resourceId);
		if(roleResourceMapper.select(rr).size() > 0){
			throw new Exception("该资源已经分配给了角色，不允许删除！");
		}
		UserResource ur = new UserResource();
		ur.setResourceId(resourceId);
		if(userResourceMapper.select(ur).size() > 0){
			throw new Exception("该资源已经分配给了用户，不允许删除！");
		}
		//2.校验资源下是否存在子资源，如果存在不允许删除
		Resource resource = new Resource();
		resource.setParentId(resourceId);
		if(mapper.select(resource).size() > 0){
			throw new Exception("该资源下存在子资源，不允许删除！");
		}
		//3.删除角色对应的资源
		//roleResourceMapper.delete(rr);
		//4.删除用户对应的资源
		//userResourceMapper.delete(ur);
		return super.delete(resourceId);
	}

	/**
	 * 根据用户ID查找对应的资源
	 * 
	 * @param userId
	 * @return
	 */
	public List<Resource> findResourceByUserId(Long userId) {
		return mapper.selectResourceByUserId(userId);
	}
	
	/**
	 * 根据角色ID查找对应的资源
	 * 
	 * @param userId
	 * @return
	 */
	public List<Resource> findResourceByRoleId(Long roleId) {
		return mapper.selectResourceByRoleId(roleId);
	}

	/**
	 * 根据用户ID查找对应的权限
	 * @param userId
	 * @return
	 */
	public Set<String> findUserPermissionsByUserId(Long userId) {
		Set<String> permissions = new HashSet<String>();
		List<Resource> resourceList = findResourceByUserId(userId);
		for (Resource resource : resourceList) {
			permissions.add(resource.getResourcePermission());
		}
		return permissions;
	}

	
	/**
	 * 根据用户ID查找对应的资源
	 * @param userId
	 * @return
	 */
	public Map<String,List<Resource>> findUserResourceByUserId(Long userId) {
		List<Resource> resourceList = findResourceByUserId(userId);
		List<Resource> moduleList = new ArrayList<Resource>();
		List<Resource> menuList = new ArrayList<Resource>();
		//查询出所有模块
		for (Resource resource : resourceList) {
			if ("MODULE".equals(resource.getResourceTypeCode())) {
				moduleList.add(resource);
			}
		}
		//查询出所有菜单
		for (Resource resource : resourceList) {
			if ("MENU".equals(resource.getResourceTypeCode())) {
				menuList.add(resource);
			}
		}
		
		Map<String,List<Resource>> treeMap = new LinkedHashMap<String,List<Resource>>();
		for (Resource module : moduleList) {
			List<Resource> list = new ArrayList<Resource>();
			for(Resource menu : menuList){
				if(menu.getParentId() == module.getResourceId()){
					list.add(menu);
				}
			}
			treeMap.put(module.getResourceName(), list);
		}
		return treeMap;
	}
	
	/**
	 * 根据用户ID查找对应的菜单
	 * @param userId
	 * @return
	 */
	public List<Resource> findUserMenusByUserId(Long userId) {
		List<Resource> resourceList = findResourceByUserId(userId);
		List<Resource> menuList = new ArrayList<Resource>();
		for (Resource resource : resourceList) {
			if (resource.isRootNode()) {
				continue;
			}
			if (!"MENU".equals(resource.getResourceTypeCode())) {
				continue;
			}
			menuList.add(resource);
		}
		return menuList;
	}
	
	/**
	 * 查询所有的模块和菜单
	 * @return
	 */
	public List<Resource> findModelsAndMenus() {
		List<Resource> resourceList = findAll();
		List<Resource> menuList = new ArrayList<Resource>();
		for (Resource resource : resourceList) {
			if (resource.isRootNode()) {
				continue;
			}
			if ("BUTTON".equals(resource.getResourceTypeCode())) {
				continue;
			}
			menuList.add(resource);
		}
		return menuList;
	}
	
	public List<Resource> getResourceTree(List<Resource> resourceList,List<Resource> accessResourceList){
		for(Resource resource : resourceList){
			for(Resource accessResource : accessResourceList){
				if(resource.getResourceId() == accessResource.getResourceId()){
					resource.setChecked(true);
					break;
				}
			}
		}
		return resourceList;
	}
	
	public String[] findResourceIds(List<Resource> resourceList){
		
		List<String> list = new ArrayList<String>();
		for(Resource resource : resourceList){
			list.add(resource.getResourceId()+"");
		}
		return (String[])list.toArray(new String[list.size()]);
	}
}
